package com.example;

import com.example.calculation.GodInference;
import com.example.utils.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author
 * @date 2021/1/18 10:53
 */


public class App{

    public static void main(String[] args) {
        try {
            defaultTask();
            // testTask();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void defaultTask() throws IOException, Exception {

        double[] reward = new double[10000];
        int[] materialCount = new int[47];
        //将倍率不明菜谱设置成 -100，防止被使用
        for (int i = 0; i < reward.length; i++) {
            reward[i] = -100;
        }

      //  URL url = new URL("https://bcjh.oschina.io/data/foodRule.min.json?v=5");
        //URL url = new URL("https://bcjh.xyz/api/get_rule?time=2020-12-18T05%3A00%3A00.000Z");
        URL url = new URL("https://bcjh.xyz/api/get_rule");

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36");
        //设置请求方式
        urlConnection.setRequestMethod("GET");
        //连接
        urlConnection.connect();
        //得到响应码
        int responseCode = urlConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            //得到响应流
            InputStream inputStream = urlConnection.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            String reponse = sb.toString();

            System.out.println(reponse);
            ObjectNode jsonObject = JSON.getObjectMapper().readValue(reponse, ObjectNode.class);
            JsonNode rules = jsonObject.get("rules");
            for (int i = 0; i < rules.size(); i++) {
                JsonNode jsonObject1 = rules.get(i);
                //湖区规则的标题，
                String title = jsonObject1.get("Title").asText();
                if (title != null && !title.isEmpty()) {
                    if (title.contains("御前")) {

                        // 解析菜谱的奖励倍数
                        ObjectNode repriceEffect = (ObjectNode) jsonObject1.get("RecipeEffect");

                        Iterator<Map.Entry<String, JsonNode>> fields = repriceEffect.fields();

                        fields.forEachRemaining(new Consumer<Map.Entry<String, JsonNode>>(){
                            @Override
                            public void accept(Map.Entry<String, JsonNode> stringJsonNodeEntry) {
                                reward[Integer.valueOf(stringJsonNodeEntry.getKey())] = stringJsonNodeEntry.getValue().asDouble();
                            }
                        });

                        if (jsonObject1.get("MaterialsLimit").isInt()) {
                            int materialsLimit = jsonObject1.get("MaterialsLimit").asInt();
                            for (int i2 = 0; i2 < materialCount.length; i2++) {
                                materialCount[i2] = materialsLimit;
                            }
                        } else {
                            ObjectNode materials = (ObjectNode) jsonObject1.get("MaterialsLimit");
                            materials.fields().forEachRemaining(new Consumer<Map.Entry<String, JsonNode>>(){
                                @Override
                                public void accept(Map.Entry<String, JsonNode> stringJsonNodeEntry) {
                                    Integer materialID = Integer.valueOf(stringJsonNodeEntry.getKey());
                                    Integer count = stringJsonNodeEntry.getValue().asInt();
                                    materialCount[materialID] = count;
                                }
                            });
                        }
                        break;
                    }
                }
            }
        } else {
            throw new Exception(" 404 未获取到数据");
        }
        GodInference inference = new GodInference(reward, materialCount);
        inference.refere();
    }


    public static void testTask() throws InterruptedException {
        double[] reward = new double[10000];
        int[] materialCount = new int[47];
        for (int i = 0; i < reward.length; i++) {
            reward[i] = 1;
        }
        for (int i = 0; i < materialCount.length; i++) {
            materialCount[i] = 50;
        }
        GodInference inference = new GodInference(reward, materialCount);
        inference.refere();
    }

}
